export default function () {
    angular.module('ui.table')
        .directive('searchDate', function (eventHelper) {
            return {
                restrict: 'E',
                replace: true,
                transclude: true,
                controller:function ($scope) {

                },
                require:"^?uiForm",
                scope:{
                    value:'@' //自动绑定，传递的是字符串
                },
                template: function(el,attr){
                    const placeholder=attr.placeholder?attr.placeholder:"";
                    const key=attr.key||attr.name;
                    return '<div class="'+attr.class+'" ><input class="form-control" type="text"   id="datetimepicker_'+key+'"readonly placeholder="'+placeholder+'" name='+attr.name+'></div>';
                },
                link : function(scope, element, attrs,ctrl,transcludeFn) {

                    const format=attrs.format||"yyyy-mm-dd hh:ii:ss";
                    let datepicker=null;

                    if(format.indexOf('ii')!=-1||format.indexOf('ss')!=-1){
                        datepicker=$(element).find("input").datetimepicker({
                            format:format,
                            autoclose: true,
                            todayBtn: true,
                            pickerPosition: "bottom-left"
                        });
                    }else if(format.indexOf('hh')!=-1){
                        datepicker=$(element).find("input").datetimepicker({
                            startView:'day',
                            minView:'day',
                            format:format,
                            autoclose: true,
                            todayBtn: true,
                            pickerPosition: "bottom-left"
                        });
                    }else if(format.indexOf('dd')!=-1){
                        datepicker=$(element).find("input").datetimepicker({
                            startView:'month',
                            minView:'month',
                            format:format,
                            autoclose: true,
                            todayBtn: true,
                            pickerPosition: "bottom-left"
                        });
                    }else if(format.indexOf('mm')!=-1){
                        datepicker=$(element).find("input").datetimepicker({
                            startView:'year',
                            minView:'year',
                            format:format,
                            autoclose: true,
                            todayBtn: true,
                            pickerPosition: "bottom-left"
                        });
                    }
                    scope.$watch('value',function (newValue,oldValue) {
                        datepicker && datepicker.datetimepicker('update',newValue);
                    });
                },
            };
        });
}